package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.pojo.Permission;
import com.neuedu.mapper.PermissionMapper;
import com.neuedu.pojo.RolePermission;
import com.neuedu.pojo.User;
import com.neuedu.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.service.IRolePermissionService;
import com.neuedu.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author wgq
 * @since 2020-09-18
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {
    @Resource
    IUserService userService;
    @Resource
    IRolePermissionService rolePermissionService;

    @Override
    public Object list(Permission Permission) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", 0);
        IPage<Permission> page = this.page(new Page<>(Permission.getPageNo(), Permission.getPageSize()), wrapper);
        List<Permission> list = page.getRecords();
        for (Permission permission : list) {
            getChildren(permission);
        }
        return page;
    }

    @Override
    public boolean add(Permission Permission) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.eq("name", Permission.getName());
        Permission query = this.getOne(wrapper);
        if (query == null)
            return this.save(Permission);
        return false;
    }


    /**
     * 参数为父权限对象
     * 通过父权限获取子权限
     */
    private void getChildren(Permission permission) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", permission.getId());
        List<Permission> list = this.list(wrapper);
        for (Permission p : list) {
//            if(permission.getType() != 0) {
            getChildren(p);
//            }
        }
        permission.setChildren(list);
    }


    @Override
    public List<Permission> userPermissionList(Integer userId) {
        //1根据用户id查询出所有的角色ums_user_role
        User user=userService.getById(userId);

        //根据角色获取权限id集合
        QueryWrapper<RolePermission> RolePermissionWrapper = new QueryWrapper<>();
        RolePermissionWrapper.eq("role_id", user.getUserType());
        List<RolePermission> rolePermissionList = rolePermissionService.list(RolePermissionWrapper);
        if (rolePermissionList == null) {
            return null;
        }

        Set permissionIdSet = new HashSet();
        for (RolePermission RolePermission : rolePermissionList) {
            permissionIdSet.addAll(Arrays.asList(RolePermission.getPermissionId().split(",")));
        }

//        permissionIdSet.forEach(System.out::println);

        //根据权限id查询权限列表
        QueryWrapper<Permission> PermissionQueryWrapper = new QueryWrapper<>();
        PermissionQueryWrapper.in("id", permissionIdSet);
        List<Permission> allPermission = this.list(PermissionQueryWrapper);
        //目标，将顶层菜单放入
        List<Permission> frontPermissionList = new ArrayList();
        for (Permission permission : allPermission) {
            if (permission.getParentId() == 0) {
                setChildrenPermission(permission, allPermission);
                frontPermissionList.add(permission);
            }
        }
        return frontPermissionList;
    }

    private void setChildrenPermission(Permission permission, List<Permission> allPermission) {
        List<Permission> children = new ArrayList();
        for (Permission Permission : allPermission) {
            if (permission.getId() == Permission.getParentId()) {
                setChildrenPermission(Permission, allPermission);
                children.add(Permission);
            }
        }
        permission.setChildren(children);
    }


    @Override
    public List<Permission> userPermissionListValidate(Integer userId) {
        //1根据用户id查询出所有的角色ums_user_role
        User user=userService.getById(userId);

        //根据角色获取权限id集合
        QueryWrapper<RolePermission> RolePermissionWrapper = new QueryWrapper<>();
        RolePermissionWrapper.eq("role_id", user.getUserType());
        //根据角色获取权限id集合
        List<RolePermission> rolePermissionList = rolePermissionService.list(RolePermissionWrapper);
        if (rolePermissionList == null) {
            return null;
        }

        Set permissionIdSet = new HashSet();
        for (RolePermission RolePermission : rolePermissionList) {
            permissionIdSet.addAll(Arrays.asList(RolePermission.getPermissionId().split(",")));
        }

        //根据权限id查询权限列表
        QueryWrapper<Permission> PermissionQueryWrapper = new QueryWrapper<>();
        PermissionQueryWrapper.in("id", permissionIdSet);
        List<Permission> allPermission = this.list(PermissionQueryWrapper);
        return allPermission;
    }
}
